<?
session_start();
$session=$_SESSION['JID'];
$time=time();
$time_check=$time-120; //SET TIME 10 Minute

$host="localhost"; // Host name
$username="sapmenet_admin"; // Mysql username
$password="a1s2d3f4g5h6j7"; // Mysql password
$db_name="sapmenet_users"; // Database name
$tbl_name="onlineusers"; // Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
$sql2="UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}

$sql3="SELECT onlineusers.session, concat(users.FirstName, ' ', users.LastName) AS FullName, users.ID FROM $tbl_name JOIN users ON onlineusers.session=users.ID";
$result3=mysql_query($sql3);
$friends=mysql_query("SELECT friendID FROM friendships WHERE userID={$_SESSION['JID']}");
$i=0;
while ($users=mysql_fetch_array($friends, MYSQL_NUM))
{
	$frs[$i]=$users[0];
	$i++;
}
while ($users=mysql_fetch_array($result3, MYSQL_NUM))
{
	foreach($frs AS $fr)
	{
		if ($users[0]==$fr)
		{
			echo ($users[0].",".$users[1]);
		}
	}
}

// if over 10 minute, delete session 
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

mysql_close();

// Open multiple browser page for result
?>